package com.fxd.crm.web.controller.studentCourseOrder;

import com.fxd.crm.model.CourseOrder;
import com.fxd.crm.model.PageResult;
import com.fxd.crm.model.course;
import com.fxd.crm.model.curdResult;
import com.fxd.crm.service.ICourseOrderBaseService;
import com.fxd.crm.service.ICourseOrderService;
import com.fxd.crm.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @auther fuxudong
 * @date 2021/4/3 - 14:03
 */
@RestController
@RequestMapping("/studentCourseOrder")
public class studentCourseOrderBodyController {
    @Autowired
    ICourseOrderService courseOrderImpl;

    @Autowired
    ICourseService courseService;

    /**
     * page:显示当前页
     * limit：每次显示多少条
     *
     * @return
     */
    @RequestMapping("listjson")
    public PageResult<CourseOrder> listJson(HttpServletRequest request, int page, int limit,String course_name) {
        System.out.println(course_name);
        String name = (String) request.getSession().getAttribute("name");
        PageResult<CourseOrder> pageResult = courseOrderImpl.findPageResultByName(name, page, limit);
        return pageResult;
    }

    @RequestMapping("save")
    public curdResult save(CourseOrder order) {
        curdResult curdResult;
        if (order.getOrder_id() != null) {
            curdResult = new curdResult(1, "成功");
            courseOrderImpl.update(order);
        } else {
            curdResult = new curdResult(1, "成功");
            courseOrderImpl.save(order);
        }
        return curdResult;
    }

    @RequestMapping("stusave")
    public curdResult stusave(Integer course_id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        course course = courseService.findByOrderId(course_id);
        CourseOrder order = new CourseOrder();
        order.setName((String) session.getAttribute("name"));
        order.setCourse_name(course.getCourse_name());
        order.setCourse_price(course.getCourse_price());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        order.setOrder_date(df.format(new Date()));// new Date()为获取当前系统时间
        order.setWechat_no("130****2254");
        curdResult curdResult;
        curdResult = new curdResult(1, "成功");
        courseOrderImpl.save(order);
        return curdResult;
    }

    @RequestMapping("delete")
    public curdResult delete(Integer order_id) {
        curdResult curdResult = new curdResult();
        courseOrderImpl.deleteByOrderId(order_id);
        return curdResult;

    }


}
